package com.sunyur.tailor.channel.service.middle.purchase.impl;

import com.sunyur.tailor.channel.common.inner.base.MsgResultCode;
import com.sunyur.tailor.channel.common.middle.base.ChannelListResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.purchase.model.PurchaserApplyHeadModel;
import com.sunyur.tailor.channel.service.inner.purchase.model.PurchaserApplyItemModel;
import com.sunyur.tailor.channel.service.inner.purchase.param.PurchaseApplyHeadByCodeParam;
import com.sunyur.tailor.channel.service.inner.purchase.param.PurchaseApplyItemsByCodeParam;
import com.sunyur.tailor.channel.service.inner.purchase.service.SunyurPurchaseApplyService;
import com.sunyur.tailor.channel.service.inner.workflow.param.WorkFlowSyncTaskParam;
import com.sunyur.tailor.channel.service.inner.workflow.service.SuryurWorkFlowService;
import com.sunyur.tailor.channel.service.middle.purchase.MiddlePurchaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @Author 99288
 * @Description 外部请购实现类
 * @Date 22:32 2021/7/7
 * @Param
 * @return
**/
@Slf4j
@Service("middlePurchaseService")
public class MiddlePurchaseServiceImpl implements MiddlePurchaseService {
    /**
     *接收外部审批结果服务
     */
    @Autowired
    private SuryurWorkFlowService suryurWorkFlowService;
    @Autowired
    private SunyurPurchaseApplyService sunyurPurchaseApplyService;

    @Override
    public ChannelResult syncAudit(WorkFlowSyncTaskParam workFlowSyncTaskParam) {
        ChannelResult result = new ChannelResult<>();
        try {
            result = suryurWorkFlowService.workflowTaskComplete(workFlowSyncTaskParam);
        } catch (Exception e) {
            log.error("PurchaseApplyController audit exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }

    @Override
    public ChannelResult syncCreate(MsgResultCode msgResultCode) {
        ChannelResult result = new ChannelResult<>();
        //查询申请单头
        PurchaseApplyHeadByCodeParam purchaseApplyHeadByCodeParam = new PurchaseApplyHeadByCodeParam();
        purchaseApplyHeadByCodeParam.setApplyCode(msgResultCode.getCode());
        ChannelResult<PurchaserApplyHeadModel> purchaserApplyHeadResult = sunyurPurchaseApplyService.getPurchaserApplyHeadByCode(purchaseApplyHeadByCodeParam);
        if (!ChannelResultUtil.isSuccess(purchaserApplyHeadResult)){
            result.setError(purchaserApplyHeadResult.getCode(),purchaserApplyHeadResult.getMessage());
            return result;
        }
        //查询申请单行
        PurchaseApplyItemsByCodeParam purchaseApplyItemsByCodeParam = new PurchaseApplyItemsByCodeParam();
        purchaseApplyItemsByCodeParam.setApplyCode(msgResultCode.getCode());
        ChannelListResult<PurchaserApplyItemModel> purchaserApplyItemResult = sunyurPurchaseApplyService.getPurchaserApplyItemsHeadByCode(purchaseApplyItemsByCodeParam);
        if (!ChannelResultUtil.isSuccess(purchaserApplyItemResult)) {
            result.setError(purchaserApplyItemResult.getCode(),purchaserApplyItemResult.getMessage());
            return result;
        }
        //业务处理
        PurchaserApplyHeadModel purchaserApplyHead = purchaserApplyHeadResult.getContent();
        List<PurchaserApplyItemModel> purchaserApplyItem = purchaserApplyItemResult.getContent();

        return result;
    }

    @Override
    public ChannelResult process(MsgResultCode msgResultCode) throws Exception {
        //业务处理
        return ChannelResultUtil.success(true);
    }

    @Override
    public ChannelResult demandOperate(MsgResultCode msgResultCode) throws Exception {
        //业务处理
        return ChannelResultUtil.success(true);
    }

    /**
     * 请购创建数据实体类转换
     */
    private void conversionCreate(PurchaserApplyHeadModel purchaserApplyHead, List<PurchaserApplyItemModel> purchaserApplyItem) {

    }
}
